<?php


  function valid_domain($domain) {
  	return valid_email_simple('user@'.$domain);
  }
	
	
	
  function valid_email($email)
  {
  	return valid_email_simple($email);
  }
	
	
	
  function valid_email_simple($email)
  {
  	if (stristr($email, ',')) {
  		foreach (explode(',', str_replace(' ', '', $email)) as $email_) {
  			if (!valid_email_simple($email_)) return false;
  		}
  
  		return true;
  	} else {
  		if (eregi("^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) return true;
  		else {
  			$parts = explode('@', $email);
  			return count($parts) == 2 && valid_ip($parts[1]);
  		}
  	}
  }
	


  function valid_ip($ip)
  {
  	return preg_match("/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/", $ip);
  }



	function valid_phone($phone = false)
	{
		return ereg('^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$', $phone);
	}

	

  function valid_uri($uri, $reg_ex = true)
	{
  	if ($reg_ex) {
  		if (strlen($uri) < 8) return false;
  		return preg_match( '/^(http|https|ftp|mailto):\/\/[a-z0-9A-Z]+([\-\.]{1}[a-z0-9A-Z]+)*\.[a-zA-Z]{2,5}'.'((:[0-9]{1,5})?\/.*)?$/i' ,$uri);
  		#return preg_match( '/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}'.'((:[0-9]{1,5})?\/.*)?$/i' ,$uri);
  	} else {
  		$fp = @fopen($uri,"r");
  		if ($fp) {
  			fclose($fp);
  			return true;
  		} else {
  			return false;
  		}
  	}
  }



  function variable_name(&$var, $scope=false, $prefix = false, $suffix = false)
  {
    $vals = ($scope) ? $scope : $GLOBALS;
  	if (empty($prefix)) $prefix = md5(microtime(true));
  	if (empty($suffix)) $suffix = md5(microtime(true));
    $old = $var;
    $var = $new = $prefix.rand().$suffix;
    $vname = FALSE;
    foreach($vals as $key => $val) {
      if($val === $new) $vname = $key;
    }
    $var = $old;
    return $vname;
  }


